package com.example.jksj.bookmanagement.pro.mine.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.example.jksj.bookmanagement.pro.HomeActivity;
import com.example.jksj.bookmanagement.R;
import com.example.jksj.bookmanagement.common.MyActivity;
import com.example.jksj.bookmanagement.utils.ShareUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

public class LoginActivity extends MyActivity implements View.OnClickListener {

    private TextView tv_goRegister_login;
    private TextView btn_login;
    private EditText et_username_login;
    private EditText et_password_login;
    private CheckBox checkbox_password_login;

    private String nameuser, psw;

    private  QMUITipDialog tipDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected int getTitleBarId() {
        return R.id.tb_title_login;
    }

    @Override
    public void initView() {
        tv_goRegister_login = findViewById(R.id.tv_goRegister_login);
        tv_goRegister_login.setOnClickListener(this);
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(this);
        et_username_login = findViewById(R.id.et_username_login);
        et_password_login = findViewById(R.id.et_password_login);
        checkbox_password_login = findViewById(R.id.checkbox_password_login);

        tipDialog = new QMUITipDialog.Builder(this)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("登录中...")
                .create();
    }

    @Override
    public void initData() {
        boolean isSave = ShareUtils.getBoolean("login_check", false);
        if (isSave){
            checkbox_password_login.setChecked(isSave);
            et_username_login.setText(ShareUtils.getString("login_username", "123"));
            et_password_login.setText(ShareUtils.getString("login_password", "123"));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_goRegister_login:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;

            case R.id.btn_login:
                login();
                break;
        }
    }

    //执行登录事件
    private void login() {
        nameuser = et_username_login.getText().toString();
        psw = et_password_login.getText().toString();
        if(!nameuser.isEmpty() && !psw.isEmpty()) {
            tipDialog.show();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    tipDialog.dismiss();
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    finish();
                }
            }, 2000);
        }else {
            toast("账号或密码不能为空");
        }
    }

    //用于判断是否保存登录信息
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ShareUtils.putBoolean("login_check", checkbox_password_login.isChecked());
        if (checkbox_password_login.isChecked()){
            ShareUtils.putString("login_username", et_username_login.getText().toString());
            ShareUtils.putString("login_password", et_password_login.getText().toString());
        }else {
            ShareUtils.deletedShare("login_username");
            ShareUtils.deletedShare("login_password");
        }
    }
}
